package com.tuniondata.netty.common;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * @author yuxinchao
 */
public class NamedThreadFactory implements ThreadFactory {

    private final String prefix;
    private boolean daemon = Thread.currentThread().isDaemon();

    private final AtomicInteger id = new AtomicInteger();

    public NamedThreadFactory(String prefix) {
        this.prefix = prefix;
    }

    public NamedThreadFactory(String prefix, boolean daemon) {
        this.prefix = prefix;
        this.daemon = daemon;
    }

    @Override
    public Thread newThread(Runnable r) {
        String name = String.format("%s-%d", prefix, id.getAndIncrement());
        Thread thread = new Thread(r, name);
        thread.setDaemon(daemon);
        return thread;
    }
}

